﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BestTrader.Cms.Admin.Models;
using BestTrader.Cms.Admin.Filter;
using BestTrader.Cms.Core.Helper;
using BestTrader.Cms.IServices;
using System.Security.Claims;
using BestTrader.Cms.ViewModels;
using FluentValidation.Results;
using BestTrader.Cms.Admin.Validation;
using Microsoft.AspNetCore.Authorization;

namespace BestTrader.Cms.Admin.Controllers
{
    /// <summary>
    /// 汇率设置
    /// </summary>
    [Authorize]
    public class ExchangeRateController : BaseController
    {
        public String UserId { get { return User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value; } }
        public String RoleId { get { return User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; } }
        private readonly IExchangeRateService _service;
        private readonly IManagerService _managerService;
        private readonly ICacheService _cacheService;
        public ExchangeRateController(IExchangeRateService service, IManagerService managerService, ICacheService cacheService)
        {
            this._service = service;
            this._managerService = managerService;
            this._cacheService = cacheService;
        }

        /// <summary>
        /// 主界面
        /// </summary>
        public IActionResult Index() => View();

        public string LoadData([FromQuery]ExchangeRateRequestModel model)
        {
            var user = User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value;
            var role = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value;
            return JsonHelper.ObjectToJSON(_service.LoadData(model, user, role));
        }

        [HttpGet]
        public IActionResult Setting()
        {
            ViewData["CurrencyTypes"] = _cacheService.GetCacheCurrencyTypes();

            return View();
        }

        [HttpPost, ValidateAntiForgeryToken]
        public string Setting([FromForm]ExchangeRateSettingModel item)
        {
            var result = new BaseResult();
            ExchangeRateSettingValidation validationRules = new ExchangeRateSettingValidation();
            ValidationResult results = validationRules.Validate(item);
            if (results.IsValid)
            {
                var userId = User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value;
                result = _service.Setting(item, userId);
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = results.ToString("||");
            }
            return JsonHelper.ObjectToJSON(result);
        }

        [HttpGet]
        public IActionResult AddOrModify()
        {
            ViewData["AgentManagers"] = _managerService.GetSelfAndChildManagerByManagerIdAndRoleId(UserId, RoleId, true);
            ViewData["CurrencyTypes"] = _cacheService.GetCacheCurrencyTypes();

            return View();
        }

        [HttpPost, ValidateAntiForgeryToken]
        public string AddOrModify([FromForm]ExchangeRateAddOrModifyModel item)
        {
            var result = new BaseResult();
            ExchangeRateValidation validationRules = new ExchangeRateValidation();
            ValidationResult results = validationRules.Validate(item);
            if (results.IsValid)
            {
                var userId = User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value;
                result = _service.AddOrModify(item, userId);
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = results.ToString("||");
            }
            return JsonHelper.ObjectToJSON(result);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public string Delete(int[] id)
        {
            return JsonHelper.ObjectToJSON(_service.DeleteIds(id));
        }

    }
}
